/* ------------------------------------------------------------------- Problem set #5 Source code for one layer network ------------------------------------------------------------------- */ #include "g.hh" #include "network.hh" // A matrix for storing weights which will multiply the inputs. // Even though it starts off as 0x0, it is resized by initialize() // to be (output_units) by (input_units + 1). The '+ 1' is because // a bias unit should be added to the input layer. static matrix input_weights(0, 0); // Set up some useful variables which will be set to the number of // input units, output units and the total number of weights. Note // that input_units does not count the bias unit. static int input_units = 0; static int output_units = 0; int total_weights = 0; // Initialize weight matrices, must be called in main() // PROVIDED void initialize(int inputs, int outputs) { input_units = inputs; output_units = outputs; input_weights.resize(output_units, input_units + 1); total_weights = input_weights.rows() * input_weights.columns(); } // Randomize weight matices, should be called in main() // YOU WRITE void randomize_weights(double range) { // Your code here // Set elements of input_weights to random numbers } // Calculate partial derviative of output with respect to weights // Weights represented as one big vector // YOU WRITE matrix model_gradient(vector& input_data) { // Declare matrix to fit gradient matrix gradient(output_units, total_weights); // Add bias unit with routine augment() vector augmented_input_data = augment(input_data); // Your code here // Return result return gradient; } // Calculate output of model given input // YOU WRITE vector model_output(vector& input_data) { // Your code here } // Increment the weights by delta_w, which is encoded in the vector // representation of the weights // YOU WRITE void increment_weights(vector& delta_w) { // Your code here } // Print out the details of the model // PROVIDED void print_model() { cout << "One layer sigmoid network\n"; cout << "Input units (not counting bias unit): " << input_units << "\n"; cout << "Output units: " << output_units << "\n\n"; for (int i = 0; i < output_units; i++) cout << "Output unit #" << (i + 1) << " = " << "g([" << input_weights[i].to_string() << "]^T * [inputs, 1])\n"; cout << "\n"; } // For two layer net only, return the value of the hidden units // PROVIDED vector hidden_output(vector& input_data) { vector v(0); cerr << "No hidden layer defined for one layer network\n"; return v; }